<?php
require_once 'Zend/Db/Table/Abstract.php';
class TeacherModel extends Zend_Db_Table_Abstract
{
	protected $_name = 'teachers';
	protected $_id = 'id';
	
	
		/*
	 * Lay danh sach cac sinh vien
	 */
	public static function getAllTeacher(){
		$db = Zend_Registry::get('connectDB');
		$mySql = $db->select()
					->from('teachers');
		$list = $db->fetchAll($mySql);
		return $list;
	}
	
	/*
	 * Lay danh sach sinh vien co gioi han so luog
	 */
	
	public function selectLimitTeacher($count,$limit){
	//	echo "swswq";die;
		$db = Zend_Registry::get('connectDB');
		$mysql = $db -> select()
					->from('teachers')
                    ->join('institutes', 'institutes.id = teachers.id_institutes',array('i_name' => 'institutes.name'))
					->limit($count,$limit)
					->order("id DESC");
		$list = $db->fetchAll($mysql);
	//	echo $mysql->__toString();exit();
		return  $list;
	}
	
	/*
     * Ham lay thong tin sinh vien qua id
     */  
    public function getDbbyID($id){
        $db= Zend_Registry::get('connectDB');
        $mysql= $db->select()
            ->from($this->_name)
            ->where('id=?',$id);
        $list= $db->fetchAll($mysql);
        return $list;
    }
  
	
	/*
	 * Hàm upload file lên server.
	 */
	
	public function uploadFile(){
		if (!empty($_FILES)) {
		//	echo 123;die;
			$realpath = dirname(__FILE__);
			$app = APPLICATION_PATH;
			//TREN SERVER : \yome-administrator\application
			$realpath = str_replace("\application" , "" , $app);
			$realpath = str_replace("\\" , "/" , $realpath);
			$realpath = $realpath.'/public/file/excel/' ;
			
			$chars = "0123456789";
			$size = strlen( $chars );
			$str = "";
			for( $i = 0; $i < 5; $i++ ) {
				$str .= $chars[ rand( 0, $size - 1 ) ];
			}
			$text = $str."_";
			move_uploaded_file($_FILES['file']["tmp_name"],"$realpath/".$text.$_FILES['file']["name"]);
			return $text.$_FILES['file']["name"];
		}
	}
	
	/*
	 * Ham them thong tin giao vien
	 */
	public function add($gv){
		$model = new TeacherModel();
		$data = array('name'=>$gv['name'],
				'birthday'=>$gv['birthday'],
				'id_institutes'=>$gv['institutes'],
				'time_start'=>$gv['time_start'],
				'username'=>$gv['username'],
				'password'=>$gv['password'],
				'status'=>$gv['status'],
				'phone'=>$gv['phone'],
				'email'=>$gv['email']);
	//	Zend_Debug::dump($data);die;
		$id = $model->insert($data);
		return $id;
	}

	public function edit($gv,$id){
		$model = new TeacherModel();
		$data = array('name'=>$gv['name'],
				'birthday'=>$gv['birthday'],
				'id_institutes'=>$gv['institutes'],
				'time_start'=>$gv['time_start'],
				'username'=>$gv['username'],
				'password'=>$gv['password'],
				'status'=>$gv['status'],
				'phone'=>$gv['phone'],
				'email'=>$gv['email']);
	//	Zend_Debug::dump($data);die;
		$id = $model->update($data,"id=".$id);
		return $id;
	}
	
     /*
	 * Tìm kiếm .
	 */
 	public function search($id_search,$name_search){
 		$db = Zend_Registry::get('connectDB');
 		$mysql = $db ->select('id')
			 		 ->from('teachers')
 					 ->join('institutes', 'institutes.id = teachers.id_institutes', array('i_name' => 'institutes.name'));
		if($id_search){
		  $mysql->where("teachers.id_institutes =?",$id_search);
		}
        $mysql->where("teachers.name LIKE ?",'%'.$name_search.'%');
		
        
	//	echo $mysql->__toString();exit();
		$list = $db->fetchAll($mysql);
		return $list;
 	}

 	/*
	*Lay ten gv theo id
	*/
	 public function getNameById($id){
		$db = Zend_Registry::get('connectDB');
		$mySql = $db->select()
					->from('teachers')
					->where("id =?",$id);
		$list = $db->fetchAll($mySql);
		return $list[0]["name"];
	}

	public function checkExitsTeacher($gv){
		$db = Zend_Registry::get('connectDB');
		$mySql = $db->select()
					->from('teachers')
					->where("name =?",$gv["name"])
					->where("birthday =?",$gv["birthday"])
					->where("time_start =?",$gv["time_start"]);
		$list = $db->fetchAll($mySql);
		if(count($list)) return $list[0]["id"];
		else return 0;
	}
	
}
















